/*
* Copyright (c) 2013 City University of Hongkong.
*                    Cloud computing security group.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
* graph_combine.h - combine reduced graph and compute MC-STA
*
* Authors: Xingliang Yuan     
* Emails:  xyuan86@gmail.com
*
*/


#ifndef GRAPH_COMBINE_H
#define GRAPH_COMBINE_H

#include "../algorithm/mc_utility.h"
#include "../graph/ff_graph.h"

class graph_combine: public mc_utility
{
protected: 

    vector<unsigned long> mc_vertex_seed; // store random_seed

    vector <unsigned int> mapping; //store non-black vertex relabel info

    vector<unsigned int> vertex_set; // vertex_set by topological order

    //vector<float> edge_delay;

    unsigned int black_num; //black vertex num
    unsigned int black_edge_num; //black edge num

    //unsigned int colored_num;  //colored vertex number
    //unsigned int ff_num;  //ff number
    //unsigned int gate_num;  //ff number
    unsigned int delay_column;  //ff number

    //vector< vector<float> >vertex_delay; // random vertex delay by PCA
    vector<float> mc_vertex_delay; // deterministic vertex delay

    vector<float> vertex_arrive_time; // vertex arrive time

    /*  
    * description: load vertex mapping to recover the label
    * param: 
    * return: 
    */
    void load_vertex_mapping(char* map_file);

    /*  
    * description: load random gate delay for each vertex
    * param: 
    * return: 
    */
    void load_vertex_delay(char* benchmark, class ff_graph& ff_obj);

    /*  
    * description: generate deterministic vertex delay for one MC simulation
    * param: ff_graph instance 
    * return: 
    */
    void gen_mc_delay(unsigned int ith_mc, class ff_graph& ff_obj);

    /*  
    * description: load reduce graph and integrate with local black dag
    * param: 
    * return: 
    */
    void integrate_graph(FILE* fp, char* black_file, class ff_graph& ff_obj);

    /*  
    * description: STA on the reduced graph
    *              following topological order for each MC simulation
    * param: read edge delay from fp
    * return: maximum path delay 
    */
    float sta_pert_reduce(class ff_graph& ff_obj);

public:
    /*  
    * description: MC-SSTA on reduced graph
    * param: 
    * return: 
    */
    float mc_ssta_reduce(char* benchmark, unsigned int mc_num,
                    class ff_graph& ff_obj);
    
};

#endif // GRAPH_COMBINE_H

